package cn.guozeyiblog.client;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import cn.guozeyiblog.netutils.NetWorkStatus;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;


public class ImageActivity extends Activity {
	ImageView imageView;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setProgressBarVisibility(true);
		super.onCreate(savedInstanceState);
		setTitle("ImageView");
		String url = getIntent().getExtras().getString("URL", "http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png");
		imageView = new ImageView(getApplicationContext());
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		imageView.setLayoutParams(params);
		imageView.setImageDrawable(getResources().getDrawable(R.drawable.album_thumb_default));
		setContentView(imageView);
		if(NetWorkStatus.isNetworkAvailable(getApplicationContext())){
			new AsyGetImage().execute(url);
		}else {
			Toast.makeText(getApplicationContext(), "你的网络不可用哦", 0).show();
		}
		
	}
	
	public class AsyGetImage extends AsyncTask<String, Drawable, Drawable>{

		@Override
		protected Drawable doInBackground(String... params) {
			// TODO Auto-generated method stub
			Drawable drawable=null;
			try {
				drawable= Drawable.createFromStream(new URL(params[0]).openStream(), "image.png");
			}  catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			publishProgress(drawable);
			return drawable;
		}
		
		@Override
		protected void onProgressUpdate(Drawable... values) {
			// TODO Auto-generated method stub
			
             if (values[0]!=null) {
				imageView.setImageDrawable(values[0]);
				System.out.println("Update-----Image");
			}
		}
	}
	
	
}
